#include <QtGui>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include "login_ui.h"
extern "C"{
#include "message/clientConnect.h"
#include "message/clientMessage.h"
}
#define BUF_SIZE 100
#define MAX_CLNT 256
Dialog::Dialog(RoomDialog *main)
{
    rdia=main;
     
    createGridGroupBox();

    
    okButton = new QPushButton(tr("OK"));
    cancelButton = new QPushButton(tr("Exit"));
    okButton->setDefault(true);

  connect(okButton, SIGNAL(clicked()), this, SLOT(login()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch(1);
    buttonLayout->addWidget(okButton);
    buttonLayout->addWidget(cancelButton);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(gridGroupBox);
    mainLayout->addLayout(buttonLayout);
    setLayout(mainLayout);
 
  //  QObject::connect(okButton, SIGNAL(clicked() ), SLOT( click_0() ));
    setWindowTitle(tr("Login"));
}


//login button push
void Dialog::login(){
	QByteArray ba;
	ba=lineEdits[0]->text().toUtf8();
	char ip[80],strName[80];
	strcpy(ip,ba.data());
	ba=lineEdits[1]->text().toUtf8();
	const char *strPort=ba.data();
	int iPort=atoi(strPort);
	ba=lineEdits[2]->text().toUtf8();
	strcpy(strName,ba.data());
	printf("ip : %s port : %d name : %s \n",ip, iPort, strName);
	int r;
	r=connect2Server(ip, iPort);
	if(r<0) return;
	init_clientMessage(r);
	pthread_t reader_tid;
	if(pthread_create(&reader_tid,NULL,receiver,(void*)r)!=0){
		perror("pthread_create");
		return;
	}
	enterUser(strName);
	rdia->reflushRoom();
rdia->show();
this->hide();
}

//exit button push
void Dialog::exit(){

}



void Dialog::createGridGroupBox()
{
    gridGroupBox = new QGroupBox(tr("input window"));
    QGridLayout *layout = new QGridLayout;

	labels[0] = new QLabel(tr("IP"));
	labels[1] = new QLabel(tr("PORT"));
        labels[2] = new QLabel(tr("ID"));
      
           
        lineEdits[0] = new QLineEdit;
        lineEdits[1] = new QLineEdit;
        lineEdits[2] = new QLineEdit;
	lineEdits[0]->setText("127.0.0.1");
	lineEdits[1]->setText("4000");
	layout->addWidget(labels[0],  0, 0);
	layout->addWidget(lineEdits[0],  0, 1);
        layout->addWidget(labels[1], 1, 0);
        layout->addWidget(lineEdits[1], 1, 1);
        layout->addWidget(labels[2], 2, 0);
        layout->addWidget(lineEdits[2], 2, 1);

gridGroupBox->setLayout(layout);
}







